//
//  JPNetWorkService.m
//  GrainAndOil
//
//  Created by xiangming on 14-7-9.
//  Copyright (c) 2014年 JunePartner. All rights reserved.
//

#import "NetWorkService.h"

@implementation NetWorkService

+ (Request *)byRequestWithAPIPrefix:(NSString *)prefix
                                andAPISuffix:(NSString *)suffix
                               andHttpMethod:(NSString *)httpMethod
                               andParameters:(NSMutableDictionary *)params
                             andRequestCache:(BOOL)requestCache
                           andUseParamDic:(BOOL)useParamDic
                       andRequestFinishBlock:(BYRequestFinishBlock)finishBlock
{
    //初始化网络请求
    Request *request = [[Request alloc] initWithParamDic:params
                                                andApiPrefix:prefix
                                                andApiSuffix:suffix
                                               andHttpMethod:httpMethod
                                             andRequestCache:requestCache
                                              andUseParamDic:useParamDic];

    //设置代理
    [request setFinishBlock:finishBlock];
    //发送异步请求
    [request sendAsynchronous];
    return request;
}

+ (Request *)byRequestWithAPIPrefix:(NSString *)prefix
                         andAPISuffix:(NSString *)suffix
                        andHttpMethod:(NSString *)httpMethod
                        andParameters:(NSMutableDictionary *)params
                      andRequestCache:(BOOL)requestCache
                       andUseParamDic:(BOOL)useParamDic
                          andFileData:(NSData *)fileData
                           andFileKey:(NSString *)fileKey
                andRequestFinishBlock:(BYRequestFinishBlock)finishBlock
{
    //初始化网络请求
    Request *request = [[Request alloc] initWithParamDic:params
                                                andApiPrefix:prefix
                                                andApiSuffix:suffix
                                               andHttpMethod:httpMethod
                                             andRequestCache:requestCache
                                              andUseParamDic:useParamDic];
    [request addData:fileData forKey:fileKey];
    //设置代理
    [request setFinishBlock:finishBlock];
    //发送异步请求
    [request sendAsynchronous];
    return request;
}

+ (Request *)byRequestWithAPIPrefix:(NSString *)prefix
                         andAPISuffix:(NSString *)suffix
                        andHttpMethod:(NSString *)httpMethod
                        andParameters:(NSMutableDictionary *)params
                      andRequestCache:(BOOL)requestCache
                       andUseParamDic:(BOOL)useParamDic
                          andFilePath:(NSString *)filePath
                           andFileKey:(NSString *)fileKey
                andRequestFinishBlock:(BYRequestFinishBlock)finishBlock
{
    //初始化网络请求
    Request *request = [[Request alloc] initWithParamDic:params
                                                andApiPrefix:prefix
                                                andApiSuffix:suffix
                                               andHttpMethod:httpMethod
                                             andRequestCache:requestCache
                                              andUseParamDic:useParamDic];
    [request addFile:filePath forKey:fileKey];
    //设置代理
    [request setFinishBlock:finishBlock];
    //发送异步请求
    [request sendAsynchronous];
    return request;
}



//带参数编码格式请求数据
+ (Request *)byRequestWithAPIPrefix:(NSString *)prefix
                       andAPISuffix:(NSString *)suffix
                      andHttpMethod:(NSString *)httpMethod
                      andParameters:(NSMutableDictionary *)params
                    andRequestCache:(BOOL)requestCache
                     andUseParamDic:(BOOL)useParamDic
                andPostDataEncoding:(MKNKPostDataEncodingType)encoding
              andRequestFinishBlock:(BYRequestFinishBlock)finishBlock
{
    //初始化网络请求
    Request *request = [[Request alloc] initWithParamDic:params
                                            andApiPrefix:prefix
                                            andApiSuffix:suffix
                                           andHttpMethod:httpMethod
                                         andRequestCache:requestCache
                                          andUseParamDic:useParamDic];
    //设置代理
    [request setFinishBlock:finishBlock];
    //发送异步请求
    request.postDataEncoding = encoding;
    [request sendAsynchronous];
    return request;
}

+ (NetworkStatus)networkStatus
{
    //    return NotReachable;
    Reachability * reachbility = [Reachability reachabilityForInternetConnection];
    NetworkStatus status = [reachbility currentReachabilityStatus];
    return status;
}

@end
